package yqt.com.cnki.controller;


import haha.life.common.CutPageBean;
import haha.life.normal.TokenUtils;
import jzqb.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yqt.com.cnki.Common.Tool;
import yqt.com.cnki.bo.CollectionBo;
import yqt.com.cnki.bo.DownBo;
import yqt.com.cnki.bo.ViewBo;
import yqt.com.cnki.service.PersonCenterService;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/personCenter")
public class PersonCenterController {

    @Autowired
    Tool sysTool;

    @Autowired
    PersonCenterService personCenterService;

    //获取最近的收藏List
    @PostMapping("/getCollectionList")
    public Result getCollectionList(@RequestBody CollectionBo collectionBo , HttpServletRequest httpServletRequest) {
        String usrId = TokenUtils.getCurrentUserId(httpServletRequest);
        collectionBo.setUserId(usrId);
        CutPageBean<CollectionBo> collectionBoCutPageBean = personCenterService.getCollectionList(collectionBo);
        return Result.succ(collectionBoCutPageBean);
    }


    //插入收藏数据
    @PostMapping("/insertCollection")
    public Result insertCollection(@RequestBody CollectionBo collectionBo, HttpServletRequest httpServletRequest) {
        String usrId = TokenUtils.getCurrentUserId(httpServletRequest);
        collectionBo.setUserId(usrId);
        if (collectionBo.getCollectClassifyID()=="" || collectionBo.getCollectClassifyID()==null) {
            return Result.fail("请选择收藏分类");
        }
        Boolean bool = personCenterService.insertCollection(collectionBo);
        if (bool) {
            return Result.succ("插入收藏成功");
        } else {
            return Result.fail("插入收藏失败");
        }
    }

    //删除收藏
    @PostMapping("/deleteCollection")
    public Result deleteCollection(@RequestBody CollectionBo collectionBo, HttpServletRequest httpServletRequest) {
        String usrId = TokenUtils.getCurrentUserId(httpServletRequest);
       
        collectionBo.setUserId(usrId);
        Boolean bool = personCenterService.deleteCollection(collectionBo);
        if (bool) {
            return Result.succ("删除收藏成功");
        } else {
            return Result.fail("删除收藏失败");
        }
    }

    //获取最近的下载List
    @PostMapping("/getDownList")
    public Result getDownList(@RequestBody DownBo downBo , HttpServletRequest httpServletRequest) {
        String usrId = TokenUtils.getCurrentUserId(httpServletRequest);
       
        downBo.setUserId(usrId);
        CutPageBean<DownBo> downBoCutPageBean = personCenterService.getDownList(downBo);
        return Result.succ(downBoCutPageBean);
    }

    //插入下载
    @PostMapping("/insertDown")
    public Result insertCollection(@RequestBody DownBo downBo, HttpServletRequest httpServletRequest) {
        String usrId = TokenUtils.getCurrentUserId(httpServletRequest);
       
        downBo.setUserId(usrId);
        Boolean bool = personCenterService.insertDown(downBo);
        if (bool) {
            return Result.succ("插入下载成功");
        } else {
            return Result.fail("插入下载失败");
        }
    }

    //删除下载
    @PostMapping("/deleteDown")
    public Result deleteCollection(@RequestBody DownBo downBo, HttpServletRequest httpServletRequest) {
        String usrId = TokenUtils.getCurrentUserId(httpServletRequest);
       
        downBo.setUserId(usrId);
        Boolean bool = personCenterService.deleteDown(downBo);
        if (bool) {
            return Result.succ("删除下载成功");
        } else {
            return Result.fail("删除下载失败");
        }
    }

    //获取最近的游览List
    @PostMapping("/getViewList")
    public Result getViewList(@RequestBody ViewBo viewBo, HttpServletRequest httpServletRequest) {
        String usrId = TokenUtils.getCurrentUserId(httpServletRequest);
        viewBo.setUserId(usrId);
        CutPageBean<ViewBo> viewBoCutPageBean = personCenterService.getViewList(viewBo);
        return Result.succ(viewBoCutPageBean);
    }


    //插入浏览
    @PostMapping("/insertView")
    public Result insertCollection(@RequestBody ViewBo viewBo, HttpServletRequest httpServletRequest) {
        String usrId = TokenUtils.getCurrentUserId(httpServletRequest);
        viewBo.setUserId(usrId);

        Boolean bool = personCenterService.insertView(viewBo);
        if (bool) {
            return Result.succ("插入浏览成功");
        } else {
            return Result.fail("插入浏览失败");
        }
    }

    //删除浏览
    @PostMapping("/deleteView")
    public Result deleteCollection(@RequestBody ViewBo viewBo, HttpServletRequest httpServletRequest) {
        String usrId = TokenUtils.getCurrentUserId(httpServletRequest);
       
        viewBo.setUserId(usrId);
        Boolean bool = personCenterService.deleteView(viewBo);
        if (bool) {
            return Result.succ("删除浏览成功");
        } else {
            return Result.fail("删除浏览失败");
        }
    }

    // updatea 自建资源的下载频次
    @PostMapping("/updateResource")
    public Result updateResource(@RequestBody DownBo downBo) {
        Boolean bool = personCenterService.updateResource(downBo);
        if (bool) {
            return Result.succ("修改下载频次成功");
        } else {
            return Result.fail("修改下载频次失败");
        }
    }
}
